package ipad.asia.controller;

import com.alibaba.fastjson.JSON;
import ipad.asia.config.StatisticalObj;
import ipad.asia.entity.Order;
import ipad.asia.service.order.OrderFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Conditional;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class StatisticalController {
    @Autowired
    private OrderFeign orderFeign;
    @RequestMapping("/statistical/data")
    public String StatisticalData(HttpSession session, Model model){
        List<Order> order =orderFeign.findByShopId((Integer) session.getAttribute("shopId"));
        List<StatisticalObj> statisticalObjList=new ArrayList<>();
        for (int i=0;i<order.size();i++){
            if(order.get(i).getPayDate()!=null) {
                StatisticalObj statisticalObj = new StatisticalObj();
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                statisticalObj.setDate(format.format(order.get(i).getPayDate()));
                statisticalObj.setPrice(order.get(i).getPrice());
                System.out.println(statisticalObj.getDate());
                System.out.println(statisticalObj.getPrice());
                statisticalObjList.add(statisticalObj);
            }else {continue;}
        }

        System.out.println("订单order："+order);
        System.out.println("订单："+statisticalObjList);
        model.addAttribute("data", JSON.toJSONString(statisticalObjList));
        System.out.println(statisticalObjList);
        System.out.println("查看json"+JSON.toJSONString(statisticalObjList));
        return "statisticalData";
    }

    @RequestMapping("/statistical/money")
    public String StatisticalMoney(HttpSession session, Model model) {
        Float total_price = 0F;
        List<Order> order =orderFeign.findByShopId((Integer) session.getAttribute("shopId"));
        List<StatisticalObj> statisticalObjList=new ArrayList<>();
        System.out.println(statisticalObjList);
        for (int i=0;i<order.size();i++) {
            if (order.get(i).getPayDate() != null) {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:MM:ss");
                StatisticalObj statisticalObj = new StatisticalObj();
                statisticalObj.setPrice(order.get(i).getPrice());
                statisticalObj.setOrderId(order.get(i).getId());
                statisticalObj.setDate2(format.format(order.get(i).getPayDate()));
                total_price = total_price + order.get(i).getPrice();
                System.out.println(total_price);
                statisticalObj.setTotal_price(total_price);
                statisticalObjList.add(statisticalObj);
            }else {continue;}}
            model.addAttribute("data", statisticalObjList);
            return "statisticalMoney";
    }

    //测试相加
    public static void main(String[] args) {
        List<Integer> l=new ArrayList<>();
        l.add(1);l.add(2);l.add(3);l.add(10);
        int tatol = 0;
        for(int i=0;i<l.size();i++){
            tatol=tatol+l.get(i);
        }
        System.out.println(tatol);
    }
}
